<?php

class Admincp_TourController extends Main_Controller_Action
{
    private $_arrParam;
    private $_model;
    private $_locale;

    public function init()
    {
        $this->loadTemplates('admin');
        $this->view->headTitle('Quản lý tour du lịch');
        $this->_arrParam = $this->_request->getParams();
        $this->_model = new Admincp_Model_Tour();
        $this->_locale = 'vi';
    }

    public function preDispatch()
    {
        $auth = Zend_Auth::getInstance();
        if(!$auth->hasIdentity()) {
            if($this->_request->getActionName() != 'login')
                $this->_redirect('admincp/index/login');
        }
    }

    public function indexAction()
    {
        if($_POST['add']) {
            $this->_redirect('admincp/tour/create');
        }
        if($_POST['delete']) {
            $listId = $this->_arrParam['check'];
            $this->_model->deleteMultiTour($listId);
            $this->_redirect($_SERVER['HTTP_REFERER']);
        }
        
        $adapter = new Zend_Paginator_Adapter_DbSelect($this->_model->listTour($this->_locale));
        $paginator = new Zend_Paginator($adapter);
        $paginator->setItemCountPerPage(40);
        $paginator->setPageRange(5);
        $currentPage = $this->_request->getParam('page', 1);
        $paginator->setCurrentPageNumber($currentPage);
        $this->view->data = $paginator;
        $this->view->total = $paginator->getTotalItemCount();
    }

    public function createAction()
    {
        $language = new Admincp_Model_Language();
        $location = new Admincp_Model_Location();
        $category = new Admincp_Model_Tourcategory();
        $this->view->language = $language->listLanguageFull();
        $this->view->location = $location->listLocationSelectbox(0);
        $this->view->category = $category->listCategorySelectbox();
        
        if($this->_request->isPost()) {
            $name['vi']     = trim(strip_tags($this->_arrParam['name-vi']));
            $name['zh']     = trim(strip_tags($this->_arrParam['name-zh']));
            $name['en']     = trim(strip_tags($this->_arrParam['name-en']));
            $name['trv']    = trim(strip_tags($this->_arrParam['name-trv']));
            $schedule['vi']  = trim(strip_tags($this->_arrParam['schedule-vi']));
            $schedule['zh']  = trim(strip_tags($this->_arrParam['schedule-zh']));
            $schedule['en']  = trim(strip_tags($this->_arrParam['schedule-en']));
            $schedule['trv'] = trim(strip_tags($this->_arrParam['schedule-trv']));
            $start_date['vi']     = trim(strip_tags($this->_arrParam['start_date-vi']));
            $start_date['zh']     = trim(strip_tags($this->_arrParam['start_date-zh']));
            $start_date['en']     = trim(strip_tags($this->_arrParam['start_date-en']));
            $start_date['trv']    = trim(strip_tags($this->_arrParam['start_date-trv']));
            $description['vi'] = trim(stripslashes($this->_arrParam['description-vi']));
            $description['zh'] = trim(stripslashes($this->_arrParam['description-zh']));
            $description['en'] = trim(stripslashes($this->_arrParam['description-en']));
            $description['trv']= trim(stripslashes($this->_arrParam['description-trv']));
            $include_price['vi']   = trim(stripslashes($this->_arrParam['include_price-vi']));
            $include_price['zh']   = trim(stripslashes($this->_arrParam['include_price-zh']));
            $include_price['en']   = trim(stripslashes($this->_arrParam['include_price-en']));
            $include_price['trv']  = trim(stripslashes($this->_arrParam['include_price-trv']));
            $not_include_price['vi']     = trim(stripslashes($this->_arrParam['not_include_price-vi']));
            $not_include_price['zh']     = trim(stripslashes($this->_arrParam['not_include_price-zh']));
            $not_include_price['en']     = trim(stripslashes($this->_arrParam['not_include_price-en']));
            $not_include_price['trv']    = trim(stripslashes($this->_arrParam['not_include_price-trv']));
            $children['vi']     = trim(stripslashes($this->_arrParam['children-vi']));
            $children['zh']     = trim(stripslashes($this->_arrParam['children-zh']));
            $children['en']     = trim(stripslashes($this->_arrParam['children-en']));
            $children['trv']    = trim(stripslashes($this->_arrParam['children-trv']));
            $condition['vi'] = trim(stripslashes($this->_arrParam['condition-vi']));
            $condition['zh'] = trim(stripslashes($this->_arrParam['condition-zh']));
            $condition['en'] = trim(stripslashes($this->_arrParam['condition-en']));
            $condition['trv']= trim(stripslashes($this->_arrParam['condition-trv']));
            $price          = (int) ($this->_arrParam['price'].'000');
            $price_children = (int) ($this->_arrParam['price_children'].'000');
            $area_id        = $this->_arrParam['area_id'];
            $location_id    = $this->_arrParam['location_id'];
            $category_id    = $this->_arrParam['category_id'];
            $upload         = new Zend_File_Transfer_Adapter_Http();
            $info           = $upload->getFileInfo();
            $image          = $upload->getFileName('image', false);
            
            // Resize Image
            $resize = new Zend_Filter_ImageSize();
            preg_match('#\.([^\.]+)$#', $image, $matches);
            $fileExtension  = $matches[1];
            $fileName = 'pic_' . time() . mt_rand(5, 99999999);
            $newFileName1 = $fileName . '.' . $fileExtension;
            
            $options = array('target' => BASE_PATH . '/public/files/tour/' . $newFileName1,'overwrite'=>true);
            $upload->addFilter('Rename',$options,'image');
            $upload->receive('image');
            $output = $resize->setHeight(400)
                            ->setWidth(500)
                            ->setThumnailDirectory(BASE_PATH . '/public/files/tour/')
                            ->filter(BASE_PATH . '/public/files/tour/' . $newFileName1);
            header('Content-Type: image/jpeg');
            $fh = fopen($output, 'r');
            fpassthru($fh);
            fclose($fh);
            $newFileName = $fileName . '-500x400.' . strtolower($fileExtension);
            unlink(PUBLIC_PATH . '/files/tour/' . $newFileName1);
            
            // Insert to database
            $strFlex = new Zendvn_Filter_RemoveCircumflex();
            $data_tour = array(
                'category_id' => $category_id,
                'location_id' => $location_id,
                'area_id' => $area_id,
                'image' => $newFileName,
                'price' => $price,
                'price_children' => $price_children,
                'status' => 2,
            );
            $tour_id = $this->_model->addTour($data_tour);
            foreach($name as $key=>$value) {
                foreach($this->view->language as $lang) {
                    if($key == $lang['locale']) {
                        $data = array(
                            'tour_id' => $tour_id,
                            'language_id' => $lang['id'],
                            'name' => $value,
                            'schedule' => $schedule[$key],
                            'start_date' => $start_date[$key],
                            'description' => $description[$key],
                            'include_price' => $include_price[$key],
                            'not_include_price' => $not_include_price[$key],
                            'children' => $children[$key],
                            'condition' => $condition[$key],
                            'name_plaintext' => $strFlex->filter(strtolower($value)),
                        );
                        $this->_model->addTourLanguage($data);
                        break;
                    }
                }
            }
            $this->_redirect('admincp/tour');
        }
    }

    public function editAction()
    {
        $id = (int) $this->_arrParam['id'];
        $language = new Admincp_Model_Language();
        $location = new Admincp_Model_Location();
        $category = new Admincp_Model_Tourcategory();
        $this->view->data = $this->_model->getTourLanguageById($id);
        $this->view->tour = $this->_model->getTourById($id);
        $this->view->language = $language->listLanguageFull();
        $this->view->location = $location->listLocationSelectbox(0);
        $this->view->category = $category->listCategorySelectbox();

        if($this->_request->isPost()) {
            $name['vi']     = trim(strip_tags($this->_arrParam['name-vi']));
            $name['zh']     = trim(strip_tags($this->_arrParam['name-zh']));
            $name['en']     = trim(strip_tags($this->_arrParam['name-en']));
            $name['trv']    = trim(strip_tags($this->_arrParam['name-trv']));
            $schedule['vi']  = trim(strip_tags($this->_arrParam['schedule-vi']));
            $schedule['zh']  = trim(strip_tags($this->_arrParam['schedule-zh']));
            $schedule['en']  = trim(strip_tags($this->_arrParam['schedule-en']));
            $schedule['trv'] = trim(strip_tags($this->_arrParam['schedule-trv']));
            $start_date['vi']     = trim(strip_tags($this->_arrParam['start_date-vi']));
            $start_date['zh']     = trim(strip_tags($this->_arrParam['start_date-zh']));
            $start_date['en']     = trim(strip_tags($this->_arrParam['start_date-en']));
            $start_date['trv']    = trim(strip_tags($this->_arrParam['start_date-trv']));
            $description['vi'] = trim(stripslashes($this->_arrParam['description-vi']));
            $description['zh'] = trim(stripslashes($this->_arrParam['description-zh']));
            $description['en'] = trim(stripslashes($this->_arrParam['description-en']));
            $description['trv']= trim(stripslashes($this->_arrParam['description-trv']));
            $include_price['vi']   = trim(stripslashes($this->_arrParam['include_price-vi']));
            $include_price['zh']   = trim(stripslashes($this->_arrParam['include_price-zh']));
            $include_price['en']   = trim(stripslashes($this->_arrParam['include_price-en']));
            $include_price['trv']  = trim(stripslashes($this->_arrParam['include_price-trv']));
            $not_include_price['vi']     = trim(stripslashes($this->_arrParam['not_include_price-vi']));
            $not_include_price['zh']     = trim(stripslashes($this->_arrParam['not_include_price-zh']));
            $not_include_price['en']     = trim(stripslashes($this->_arrParam['not_include_price-en']));
            $not_include_price['trv']    = trim(stripslashes($this->_arrParam['not_include_price-trv']));
            $children['vi']     = trim(stripslashes($this->_arrParam['children-vi']));
            $children['zh']     = trim(stripslashes($this->_arrParam['children-zh']));
            $children['en']     = trim(stripslashes($this->_arrParam['children-en']));
            $children['trv']    = trim(stripslashes($this->_arrParam['children-trv']));
            $condition['vi'] = trim(stripslashes($this->_arrParam['condition-vi']));
            $condition['zh'] = trim(stripslashes($this->_arrParam['condition-zh']));
            $condition['en'] = trim(stripslashes($this->_arrParam['condition-en']));
            $condition['trv']= trim(stripslashes($this->_arrParam['condition-trv']));
            $price          = (int) ($this->_arrParam['price'].'000');
            $price_children = (int) ($this->_arrParam['price_children'].'000');
            $area_id        = $this->_arrParam['area_id'];
            $location_id    = $this->_arrParam['location_id'];
            $category_id    = $this->_arrParam['category_id'];
            $upload         = new Zend_File_Transfer_Adapter_Http();
            $info           = $upload->getFileInfo();
            $image          = $upload->getFileName('image', false);
            
            if(!empty($image)) {
                // Resize Image
                $resize = new Zend_Filter_ImageSize();
                preg_match('#\.([^\.]+)$#', $image, $matches);
                $fileExtension  = $matches[1];
                $fileName = 'pic_' . time() . mt_rand(5, 99999999);
                $newFileName1 = $fileName . '.' . $fileExtension;
                
                $options = array('target' => BASE_PATH . '/public/files/tour/' . $newFileName1,'overwrite'=>true);
                $upload->addFilter('Rename',$options,'image');
                $upload->receive('image');
                $output = $resize->setHeight(400)
                                ->setWidth(500)
                                ->setThumnailDirectory(BASE_PATH . '/public/files/tour/')
                                ->filter(BASE_PATH . '/public/files/tour/' . $newFileName1);
                header('Content-Type: image/jpeg');
                $fh = fopen($output, 'r');
                fpassthru($fh);
                fclose($fh);
                $newFileName = $fileName . '-500x400.' . strtolower($fileExtension);
                unlink(PUBLIC_PATH . '/files/tour/' . $newFileName1);
                unlink(PUBLIC_PATH . '/files/tour/' . $this->view->tour['image']);
            }
            
            // Update to database
            $strFlex = new Zendvn_Filter_RemoveCircumflex();
            $data_tour = array(
                'category_id' => $category_id,
                'location_id' => $location_id,
                'area_id' => $area_id,
                'price' => $price,
                'price_children' => $price_children,
            );
            if(!empty($image)) {
                $data_tour['image'] = $newFileName;
            }
            $this->_model->updateTour($data_tour, $id);
            foreach($name as $key=>$value) {
                foreach($this->view->language as $lang) {
                    if($key == $lang['locale']) {
                        $data = array(
                            'language_id' => $lang['id'],
                            'name' => $value,
                            'schedule' => $schedule[$key],
                            'start_date' => $start_date[$key],
                            'description' => $description[$key],
                            'include_price' => $include_price[$key],
                            'not_include_price' => $not_include_price[$key],
                            'children' => $children[$key],
                            'condition' => $condition[$key],
                            'name_plaintext' => $strFlex->filter(strtolower($value)),
                        );
                        $this->_model->updateTourLanguage($data, $id);
                        break;
                    }
                }
            }
            $this->_redirect('admincp/tour');
        }
    }

    public function deleteAction()
    {
        $id = (int) $this->_arrParam['id'];
        $this->_model->deleteTour($id);
        $this->_redirect($_SERVER['HTTP_REFERER']);
    }
    
    public function publishAction()
    {
        $id = (int) $this->_arrParam['id'];
        $status = (int) $this->_arrParam['status'];
        $status = ($status == 1) ? 2 : 1;
        $data = array(
            'status'=>$status,
        );
        $this->_model->updateTour($data, $id);
        $this->_redirect($_SERVER['HTTP_REFERER']);
    }

    public function searchAction()
    {
        $strFlex = new Zendvn_Filter_RemoveCircumflex();
        $name = strtolower(trim(strip_tags($this->_arrParam['name'])));
        $status = (int) $this->_arrParam['status'];
        $this->view->keyword = array(
            'name'=>$name,
            'status'=>$status,
        );
        $data = array(
            'name'=>(string) '%'.str_replace(' ','%',$strFlex->filter($name)).'%',
            'status'=>$status,
        );
        $adapter = new Zend_Paginator_Adapter_DbSelect($this->_model->searchTour($data, $this->_locale));
        $paginator = new Zend_Paginator($adapter);
        $paginator->setItemCountPerPage(40);
        $paginator->setPageRange(5);
        $currentPage = $this->_request->getParam('page', 1);
        $paginator->setCurrentPageNumber($currentPage);
        $this->view->data = $paginator;
        $this->view->total = $paginator->getTotalItemCount();
    }
    
    public function loadlocationAction()
    {
        $this->_helper->layout()->disableLayout();
        $location = new Admincp_Model_Location();
        $parent = $this->_arrParam['parent'];
        if(!empty($this->_arrParam['location_id'])) {
            $this->view->location_id = $this->_arrParam['location_id'];
        }
        $this->view->location = $location->listLocationSelectbox($parent);
    }
}
